<?php
namespace app\home\controller;

use app\home\model\BannerModel;
use app\home\model\CommonModel;
use think\Controller;
use think\Request;

class Banner extends Controller
{
    public function __construct(Request $request = null)
    {
        $result = $this->_getBid();
        if(!$result){
            $this->success('未登录', 'index/login');
        }
    }
    /**
     * banner类
     * 列表
     */
    public function index(Request $request){
        //$bid = $request->get('bid');
        $bid = $this->_getBid();
        $field = 'banid,bid,pic,url,sort,createtime';
        $result = BannerModel::instance()->banlist($bid,$field);
        foreach ($result as &$val){
            $val['pic'] = uploadpath('banner',$val['pic']);
            $val['createtime'] = date('Y-m-d H:i:s',$val['createtime']);
        }
        return view('banner/index',['data'=>$result]);
    }
    /**
     *
     * 添加
     */
    public function add(){
        return view('banner/add');
    }

    /**
     * 添加数据库
     */
    public function insert(Request $request){
        $bid = $this->_getBid();
        $bid = $this->_getBid();
        $pic = CommonModel::instance()->upload('banner');
        $data = $request->post();
        $data['pic'] = $pic;
        $data['bid'] = $bid;
        $data['createtime'] = time();
        $result = BannerModel::instance()->baninster($data);
        if($result == 1){
            $this->success('添加成功', 'Banner/index');
        }else{
            $this->error('新增失败');
        }

    }
    
    /**
     * 删除
     */
    public function delete(Request $request){
        $banid = $request->get('id');
        $result = BannerModel::instance()->deleteban($banid);
        return $result;
    }
    

}
